package cn.doublefloat.crm.project.customer.controller;

import cn.doublefloat.crm.framework.web.controller.BaseController;
import cn.doublefloat.crm.framework.web.domain.JsonResult;
import cn.doublefloat.crm.framework.web.domain.TableResult;
import cn.doublefloat.crm.project.customer.domain.Customer;
import cn.doublefloat.crm.project.customer.domain.Pie;
import cn.doublefloat.crm.project.customer.service.CustomerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

//@Controller
//@ResponseBody

/**
 * 控制层
 *
 * @author 蒲万徐
 */
@RestController
@RequestMapping("/Customer")
public class CustomerController extends BaseController {

    @Resource
    private CustomerService customerService;

    /**
     * 1.查询所有
     *
     * @return
     */
    @GetMapping("/findCus")
    public TableResult findCustomer() {
        List<Customer> res = customerService.findCustomer();
        return getDataTable(res);
    }

    @GetMapping("/findById/{id}")
    public JsonResult findById(@PathVariable int id) {
        Customer res = customerService.findById(id);
        return JsonResult.success(res);
    }
    @GetMapping("/getPieData")
    public JsonResult findPie() {
        List<Pie> res = customerService.getPieData();
        for (Pie re : res) {
            System.out.println(re);
        }
        return JsonResult.success(res);
    }

    /**
     * 2.添加
     */
    @PostMapping("/addCus")
    public JsonResult addCustomer(Customer customer) {
        int res = customerService.addCustomer(customer);
        return toJson(res);
    }

    /**
     * 3.修改
     */
    @PutMapping("/updateCus")
    public JsonResult updateCustomer(Customer customer) {
        int res = customerService.updateCustomer(customer);
        return toJson(res);
    }

    /**
     * 4.删除
     */
    @DeleteMapping("/deleteCus/{id}")
    public JsonResult deleteCustomer(@PathVariable int id) {
        int res = customerService.deleteCustomer(id);
        return toJson(res);
    }

    /**
     * 5.设置状态
     */
    @PutMapping("/setCusState")
    public JsonResult setCusState(Customer customer) {
        System.out.println("+++++++++++++"+customer);
        int res = customerService.setCusState(customer);
        return toJson(res);
    }

}
